package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Setmeal;
import com.itheima.dto.SetmealDto;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐相关
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishservice;


    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<SetmealDto> save(@RequestBody SetmealDto setmealDto ){
        log.info("---新增菜品----");

        LambdaQueryWrapper<Setmeal> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getName,setmealDto.getName());
        Setmeal d1 = setmealService.getOne(queryWrapper);

        if(d1!=null){
            log.info("您添加的套餐已存在");
            return  R.error("您添加的套餐名称已存在");
        }
        //填充并且完善
        SetmealDto setmealDtoplus = setmealService.fillSetmeal(setmealDto);
        return R.success(setmealDtoplus);
    }

    /**
     * 套餐的分页查找
     *
     * @param page
     * @param pageSize
     * @return
     */
    //todo 修改后的表需要重新修改字段
    @GetMapping("/page")
    public R<Page<SetmealDto>> selectPage(Integer page, Integer pageSize, String name) {
        List<SetmealDto> dtoList = setmealService.selectPage(page, pageSize, name);
//        //查询总记录数
//        long dishCount= dishService.getCountByName(name);

        //组装数据
        Page<SetmealDto> dtoPage = new Page<>();

        dtoPage.setSize(pageSize);
        dtoPage.setCurrent(page);
        dtoPage.setRecords(dtoList);


        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        log.info("------删除菜品:{}-------",ids);
        setmealService.delete(ids);
        return R.success("删除成功");
    }
    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable String id){
        SetmealDto setmealDto = setmealService.getSetmealDto(id);
        return  R.success(setmealDto);
    }
    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping

    public R<String> update(@RequestBody SetmealDto setmealDto){
        log.info("修改菜品信息:{}",setmealDto);
        //todo
        setmealService.update(setmealDto);
        return R.success("修改菜品成功");
    }

    /**
     * 启用禁用套餐
     * @param ids
     * @return
     */
    @PostMapping ("/status")
    public R<String> updateState(String ids){
        log.info("---批量修改菜品状态:{}----",ids);
        setmealService.updateState(ids);
        return  R.success("状态修改成功");
    }





}

